/***********************************
 * ContactSupport Class - Singleton
 * maintains a hashmap of user's information in order to 
 * avoid doing a whole lot of addressbook lookups
 * 
 * without this, each data value in each message in the inbox would
 * have to be queried to be set in the list activity
 */
package edu.csuohio.mad.ssmk.subtxt;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

public class ContactSupport {

	private static ContactSupport instance = null;
	private Context context;
	private HashMap<String, SubTxtContact> contacts;

	// helper class to contain contact's id, display name, and image
	private class SubTxtContact {
		private long id;
		private String displayName;
		private Uri imageUri;
	}

	protected ContactSupport(Context c) {
		contacts = new HashMap<String, SubTxtContact>();
		context = c;
	}

	// Singleton class, obviously requires a way to get a reference to it.
	public static ContactSupport getInstance(Context c) {
		if (instance == null)
			instance = new ContactSupport(c);
		return instance;
	}

	@SuppressLint("SimpleDateFormat")
	// method to reformat the date from Epoch time to a human readable form
	public static String reformatDate(String date) {
		SimpleDateFormat formatter = new SimpleDateFormat(
				"MMM dd, yyyy  h:mm a");
		return formatter.format(Long.parseLong(date));
	}
	
	// return the contact's id
	public long getContactID(String number) {
		createContact(number);
		return contacts.get(number).id;
	}

	// return the contact's display name from the address book
	public String getDisplayName(String number) {
		createContact(number);
		return contacts.get(number).displayName;
	}

	// return the contact's image from the address book
	public Uri getImageUri(String number) {
		createContact(number);
		return contacts.get(number).imageUri;
	}

	// check to see if this contact already has an entry in the hash map
	public boolean hasContact(String number) {
		return contacts.containsKey(number);
	}

	// create a contact in the hash map
	public void createContact(String number) {

		// the contact exists in the hashmap
		if (contacts.containsKey(number))
			return;

		// add the key and a new contact to the hash map
		contacts.put(number, new SubTxtContact());

		// look up the display name and id of the contact
		String[] projection = new String[] {
				ContactsContract.PhoneLookup.DISPLAY_NAME,
				ContactsContract.PhoneLookup._ID };

		// the content uri references the builtin address book
		Uri contactUri = Uri.withAppendedPath(
				ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
				Uri.encode(number));

		// perform the query
		Cursor cursor = context.getContentResolver().query(contactUri,
				projection, null, null, null);

		// if there's a result, move to it
		if ((cursor != null) && (cursor.moveToFirst())) {

			// Get values from contacts database:
			contacts.get(number).id = cursor.getLong(cursor
					.getColumnIndex(ContactsContract.PhoneLookup._ID));

			// get the contact's display name and set it in the hash map
			contacts.get(number).displayName = cursor.getString(cursor
					.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));

			// Get photo of contactId as input uri
			contacts.get(number).imageUri = ContentUris.withAppendedId(
					ContactsContract.Contacts.CONTENT_URI,
					contacts.get(number).id);

		} else {  // we didn't find anything so just put the number in for the display name
			contacts.get(number).id = 0;
			contacts.get(number).displayName = number;
			contacts.get(number).imageUri = null;
		}
		cursor.close();
	}
}